package pl.edu.mimuw.irs.rcp.controller.action;

import java.util.List;

import org.eclipse.gef.EditPart;
import org.eclipse.jface.resource.ImageDescriptor;

import pl.edu.mimuw.irs.core.model.design.Fragmentation;
import pl.edu.mimuw.irs.core.model.design.IRSNodeInterface;
import pl.edu.mimuw.irs.core.model.design.Fragmentation.Type;
import pl.edu.mimuw.irs.core.model.petrinet.Place;
import pl.edu.mimuw.irs.core.model.petrinet.Transition;
import pl.edu.mimuw.irs.core.util.Position;
import pl.edu.mimuw.irs.rcp.controller.part.IRSNodeEditPart;

public class FragmentationAction extends AbstractSelectionAction {

	// *** Static methods

	private static String getFragmentationImage(IRSNodeInterface node, Type type) {
		String imageSource = null;
		switch (type) {
			case SEQUENCE:
				if (node instanceof Place) imageSource = "icon/place_sequence.png";
				else if (node instanceof Transition) imageSource = "icon/tran_sequence.png";
				break;
			case ALTERNATIVE:
				imageSource = "icon/alternative.png";
				break;
			case CONCURRENCY:
				imageSource = "icon/concurrency.png";
				break;
			case ACTIVITY:
				imageSource = "icon/activity.png";
				break;
			case ITERATION:
				imageSource = "icon/iteration.png";
				break;
			case NONE:
				if (node instanceof Place) imageSource = "icon/place_none.png";
				else if (node instanceof Transition) imageSource = "icon/tran_none.png";
				break;
			default:
				break;
		}
		return imageSource;
	}
	
	// *** Attributes
	
	private Fragmentation fragmentation;
	// Getters and setters
	public Fragmentation getFragmentation() {
		return fragmentation;
	}
	public void setFragmentation(Fragmentation fragmentation) {
		this.fragmentation = fragmentation;
	}
	
	// *** Constructors
	public FragmentationAction(IRSNodeEditPart nodeEditPart, Fragmentation fragmentation) {
		super(nodeEditPart, fragmentation.getType().toString(),
				ImageDescriptor.createFromFile(RequirementsEditionAction.class, 
						getFragmentationImage(nodeEditPart.getIRSNode(), fragmentation.getType())));
		setFragmentation(fragmentation);
	}
	
	// *** Methods
	
	@Override
	public void run() {
		getNodeEditPart().getIRSNode().setFragmentation(getFragmentation());
		getNodeEditPart().getIRSNode().setExpanded(true);
		
		int offset = 0;
		for (IRSNodeInterface node : getNodeEditPart().getIRSNode().getChildren()) {
			Position position = node.getPosition();
			switch (getFragmentation().getType()) {
				case SEQUENCE:
					position.setY(position.getY() + offset);// TODO : rozmieszczanie
					offset += 60;
					break;
				case CONCURRENCY:
					position.setX(position.getX() + offset);
					offset += 60;
					break;
				case ALTERNATIVE:
					position.setX(position.getX() + offset);
					offset += 60;
					break;
				case ACTIVITY:
					position.setX(position.getX() + offset);
					offset += 60;
					break;
				case ITERATION:
					position.setX(position.getX() + offset);
					offset += 60;
					break;
				default:
					break;
			}
		}
		
		refresh(getNodeEditPart().getRoot());
	}
	
	@SuppressWarnings("unchecked")
	private void refresh(EditPart editPart) {
		editPart.refresh();
		List<EditPart> children = editPart.getChildren();
		for (EditPart childEditPart : children) {
			refresh(childEditPart);
		}
	}
	
}
